
package fr.mehfile.api.update.tasks;

import fr.mehfile.api.parser.UpdateData;
import fr.mehfile.api.parser.XmlAgentParser;
import fr.mehfile.api.update.Tasks;
import fr.mehfile.api.update.TasksListener;

import java.io.InputStream;

import android.content.Context;

public class ParseXMLTask extends Tasks {

    private Context context;
    private InputStream xml;

    public ParseXMLTask(Context context, TasksListener listener, InputStream xml) {
        super(Tasks.PARSER_TASK_ID, listener);
        this.context = context;
        this.xml = xml;
    }

    @Override
    protected Object doInBackground(Object... params) {
        Object result = null;
        try {
            XmlAgentParser xmlParser = new XmlAgentParser(context);
            result = xmlParser.parse(context, xml);

        } catch (Exception e) {
            result = e;
        }
        return result;
    }

    @Override
    protected void onPostExecute(Object result) {
        super.onPostExecute(result);

        if (result instanceof UpdateData) {
            getListener().onComplete(this, result);
        }
        if (result instanceof Exception) {
            getListener().onError(this, (Exception) result);
        }
    }

}
